#!/usr/local/zend/bin/php
<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'console'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();


try 
{
    $opts = new Zend_Console_Getopt(
        array(
            'help' => 'Display the console help'
        )
    );
    
    $opts->parse();
} 
catch (Zend_Console_Getopt_Exception $e) 
{
    echo $e->getMessage() . "\n\n" . $e->getUsageMessage()."\n";
    exit;
}


$emailAccount = Zend_Registry::get('config')->console->email->codeComplex;

$config = array(
	'host'=> $emailAccount->host,
    'user' => $emailAccount->user,
    'password' => $emailAccount->password,
    'ssl' => $emailAccount->ssl
);

try {
    $mail = new Zend_Mail_Storage_Pop3($config);

    echo "Message Count: {$mail->countMessages()}\n";
    
    foreach($mail as $message)
    {
        echo "From: {$message->from} \nTo: {$message->to} \nSubject: {$message->subject}\n";
        
        $res = preg_match('/([\w ]+)<(\w+)@([\w.]+)>/' , $message->from, $matches);
        
        if($message->isMultipart())
        {
            foreach($message as $part)
            {
                Zend_Debug::dump($part->contentType, 'Content Type');
            }
        }
        else
        {
            
        }
    }
}
catch (Exception $e)
{
    echo $e->getMessage();
}

echo "\n";